﻿fl.outputPanel.clear();
var Version="0.1";
var dom = fl.createDocument();
var resourcePath=fl.browseForFolderURL("请选择素材路径：");
var swfPath=fl.browseForFolderURL("请选择生成swf文件的路径：");
var lib=dom.library;
var bitmapFolder="图素";
var mcFolder="导出类";
analyseFolder(resourcePath,"");

var xml = creatXML("ss",swfPath,Version);
FLfile.write(swfPath + "/link"+Version+".xml",xml);
dom.exportSWF(swfPath+"/UI.swf",true);
fl.saveDocument(dom,swfPath+"/UI.fla");
dom.close(false);


function analyseFolder(folderPath,libFolder)
{
        var fileList=FLfile.listFolder(folderPath,"files");
        for(var i=0;i<fileList.length;i++)
        {
                var fileName=fileList[i];
                var ind=fileName.indexOf(".");
               
                if(ind<=0||ind==(fileName.length-1))
                {
                        continue;
                }
          
                var ext=fileName.substr(ind+1,fileName.length-ind);
                ext=ext.toLowerCase();
              
                if(ext!="bmp"&&ext!="gif"&&ext!="jpg"&&ext!="jpeg"&&ext!="png")
                {
                        continue;
                }
           
                shortName=fileName.substr(0,ind);
                var filePath=folderPath+"/"+fileName;
                //fl.trace(filePath);
          
                dom.importFile(filePath,true);
              
                lib.selectItem(fileName);
             
               // lib.addItemToDocument({x:0,y:0});
                dom.selectAll();
               // dom.convertToSymbol("movie clip",shortName,"top left");
                lib.selectItem(shortName);
                lib.setItemProperty("linkageImportForRS",false);
                lib.setItemProperty("linkageExportForAS",true);
                lib.setItemProperty("linkageExportForRS",false);
                lib.setItemProperty("linkageExportInFirstFrame",true);
                lib.setItemProperty("linkageIdentifier",shortName);
                //dom.selectAll();
                //dom.deleteSelection();
                //var f=libFolder;
                //lib.newFolder(bitmapFolder+f);
               // lib.newFolder(mcFolder+f);
               // lib.moveToFolder(bitmapFolder+f,fileName);
                //lib.moveToFolder(mcFolder+f,shortName);        
        }        
   
        var folderList=FLfile.listFolder(folderPath,"directories");
        for(var j=0;j<folderList.length;j++)
        {
                analyseFolder(folderPath+"/"+folderList[j],libFolder+"/"+folderList[j]);
        }
}



function creatXML(_name,_SrcUrl,_Version)
{
	var lib = fl.getDocumentDOM().library;
	var xml='<?xml version="1.0" encoding="UTF-8"?>\n<resources'+' name="'+_name+'" version="'+_Version+'" Time="'+getNowTime()+'" url="res/swf/'+_SrcUrl+'">\n';
	var n = 0;
	for(var i = 0;i<lib.items.length;i++)
	{
		
		if(lib.items[i].linkageClassName!=null)
		{
			n++;
			xml+= '	<item id="'+ n +'" name="'+lib.items[i].linkageClassName+'"/>\n';
		
		}
	}
	xml+= '</resources>';
	fl.trace(xml);
	return xml;
}

function getNowTime() 
{
	var myDate = new Date();
	var dateStr = "";
	dateStr += myDate.getFullYear() + "-";
	dateStr += (myDate.getMonth() + 1) + "-";
	dateStr += myDate.getDate() + " ";
	dateStr += myDate.getHours() + ":";
	dateStr += myDate.getMinutes() + ":";
	dateStr += myDate.getSeconds();
	return dateStr;
}
